<?php
class Integral_ruleModel extends Model {
	public $list;
	public function getList2($id=0,$typeid=0,$map = array()){
		if($id!=0)$map['r.id'] = $id;
        if($typeid!=0)$map['r.type_id']=$typeid;    
		$re = $this-> alias('r')->join('left join __INTEGRAL_RULETYPE__ t on t.id=r.type_id')->join('left join __MEMBER__ m on r.member_id=m.id') -> where($map) -> Field('r.*,t.name as typename,m.name as username') -> select();
		if(false == $re) return null;
		$this->list=$re;
		return $this->list;
	}
    public function _list($view,$join = '', $field,$map,$rename = 'r',$pagesize=20) {
		//计算总页数
		$count = $this->alias($rename)->join($join)->where($map)->count();
		import('ORG.Util.Page');
		//实例化分页类
		$p = new Page($count, $pagesize);
		$list = $this->alias($rename)->join($join)->where($map)->Field($field)->limit($p->firstRow . ',' . $p->listRows)->select();
		$pages = $p->show();
		$_GET['list']=$list;
		$view->assign('list',$list);
		$view->assign('pages',$pages);
    }
	public function filter($view){
		
		if(!empty($_REQUEST['type_id'])){
            $map['r.type_id']=$_REQUEST['type_id'];
        }
        $map['r.site_id']=__SITEID__;
		$join='left join __INTEGRAL_RULETYPE__ t on r.type_id=t.id';
		$field='r.*,t.name as type_name';
		
		$this->_list($view,$join,$field,$map);
	}
    //获取所有规则类型下的所有规则
	public function getAllRule($fix=''){
		$map['site_id']=__SITEID__;
		if($fix!=''){
			$map['isfix']=$fix;	
		}
        $depart=M('integral_ruletype')->where($map)->select();
        $arr=array();
        foreach($depart as $val){
            $map['type_id']=$val['id'];
            $staffs=$this->where($map)->select();
            $arr[$val['name']]=$staffs;
        }
        return $arr;
    }
	//获取'所有规则'的视图
	public function getRuleView($view,$classname='rule'){
		$data=M('class_view')->where(['class'=>$classname])->getField('value');
		$list=$this->getAllRule();
		$view->assign('rulelist',$list);
		$result=$view->fetch('',$data);
		die($result);
		//die(json_encode(['statu'=>1,'result'=>$result]));
	}
	//获取积分名字
	public function getIntegralName($integral,$str='\r\n'){
		$inteDetail=[];
		foreach($integral as $value){
			foreach($value as $vo){
				if(isset($vo['id'])){
					$inteDetail[]=$this->where('id='.$vo['id'])->getField('name').$str;
				}
			}
		}
		return $inteDetail;
	}
	//获取某个值
	public function getIntegralField($integral,$field='name'){
		$inteDetail=[];
		foreach($integral as $value){
			foreach($value as $vo){
				if(isset($vo['id'])){
					$inteDetail[]=$this->where('id='.$vo['id'])->getField($field);
				}
			}
		}
		return $inteDetail;
	}
	//将事件生成的格式转化为如今积分加分的格式
	public function getIntegralFormat($integral){
		$integrals=[];
		foreach($integral as $k=>$v){
			foreach($v['integral'] as $key=>$value){
				$integrals[$i]['name']=$v['name'];
				$integrals[$i]['member_id']=$k;
				$integrals[$i]['type']=$key;
				$integrals[$i]['num']=$value;
			}
		}
		return $integrals;
	}
	//计算积分
	public function getIntegral($integral_rule){
		$arr=[];
		/*转换前
		$rule=[
			0=>[
				0=>[
					id=>1135
				]
				...
			]
			...
			1135=>[
				Ainte=10,
				Binte=11,
				vinte=12
			]
		]  
		转换后的结果：
		$arr=[
			0=>[
				Ainte=>20,
				Binte=>22,
				Vinte=>23
			]
			...
		]
		*/
		foreach($integral_rule as $v){
			foreach($v as $va){
				if(!isset($va['id'])){continue;}
				if(isset($va['id'])&&isset($integral_rule[$va['id']])){
					foreach($integral_rule[$va['id']] as $ruleK=>$ruleV){
						if(!isset($arr[$ruleK])){$arr[$ruleK]=0;}
						if(is_numeric($ruleV)){
							$arr[$ruleK]+=$ruleV;
						}else{
							$ruleV=(int)substr($ruleV,0,strpos($ruleV,'-'));
							$arr[$ruleK]+=$ruleV;
						}	
					}
				}
				foreach($va as $key=>$value){
					/*if($key=='id'){continue;}
					if(!isset($arr[$key])){$arr[$key]=0;}
					if(is_numeric($value)){
						$arr[$key]+=$value;
					}else{
						$value=(int)substr($value,0,strpos($value,'-'));
						$arr[$key]+=$value;
					}*/
					if(isset($integral_rule[$value])){
							
					}
					
					
						
				}
			}	
		}
		return $arr;
	}
}
?>